// 路由鉴权模块
import router from "@/router"
import nprogress from "nprogress"
import useUserStore from "@/store/modules/user"
import pinia from "@/store/index"

let userStore = useUserStore(pinia)
// 引入进度条的样式
import "nprogress/nprogress.css"
import { ElMessage } from "element-plus";
// 全局前置守卫
router.beforeEach((to: any, form: any, next: any) => {
    document.title = to.meta.title
    nprogress.start();
    let token = userStore.$state.token
    console.log("token",token);
    if (token) {
        if (to.path == '/login') {
            ElMessage({
                type: "warning",
                message: '请勿重复登录',
            })

            next({
                path: '/'
            })
        } else {
            next()
        }
    } else {

        if (to.path == '/login') {
            next()
        } else {
            next({
                path: '/login'
            })
        }

    }
})

// 全局后置守卫
router.afterEach((to: any, form: any) => {
    nprogress.done()
})